package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/*一个基础的servlet*/
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取虚拟路径
        String uri = req.getRequestURI();
        //通过虚拟路径获取指定的方法名称
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);


        try {
            //获取请求方法对象
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //暴力反射使得可以获取私有方法
            method.setAccessible(true);
            //调用invock方法
            method.invoke(this,req,resp);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
